package test.lyj;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import test.lyj.model.CurveNetThread;
import test.lyj.model.HttpClientThread;
import test.lyj.tool.Tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignInActivity extends Activity {
   private Button sigInButton;
   private TextView userStatus;
    private String signInAction;
   final String signInUrl = "http://co.lordor.com:888/coopera/workAttendance/";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("lyjtest", "startOnCreate");
        if (Tool.showDebugTitle) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        }
        setContentView(R.layout.sigin_oa);
        if (Tool.showDebugTitle) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
            final TextView leftText = (TextView) findViewById(R.id.left_text);
            final TextView rightText = (TextView) findViewById(R.id.right_text);
            leftText.setText("SignInActivity");
        }
        Log.d("lyjtest", "startWindowSwithe");
        sigInButton= (Button) findViewById(R.id.signIn_btn);
        userStatus= (TextView) findViewById(R.id.userStatus);
		sigInButton.setOnClickListener(signInClicker);
        List<NameValuePair> paramap=new ArrayList<NameValuePair>();
        MyApplication app = (MyApplication) this.getApplication();
       new HttpClientThread(SignInActivity.this, onCreateMessageHandler,paramap,null,signInUrl+"ShowAttendanceMachine.action",app.getHttpClient());


		
	}




   private OnClickListener signInClicker = new OnClickListener() {


        public void onClick(View v) {

            signInExcute();

        }


   };
    private void signInExcute() {
        List<NameValuePair> paramap=new ArrayList<NameValuePair>();
        MyApplication app = (MyApplication) this.getApplication();
        //线程的启动方法在NetThread 的构造函数中

        new HttpClientThread(SignInActivity.this, signInMessageHandler,paramap,null, signInUrl +signInAction,app.getHttpClient());
    }

   private Handler signInMessageHandler = new Handler() {
        public void handleMessage(Message msg) {
            Bundle b = msg.getData();
            String netresult = b.getString(Tool.TIMER_TASK_RESULT);
            switch (msg.what) {

                case Tool.TIMER_TASK_TIMEOUT:
                    Tool.showMsgDialog(SignInActivity.this, "线程超时"+netresult);
                    break;
                case Tool.TIMER_TASK_COMPELETE:

                        Intent intent = new Intent();
                        intent.setClass(SignInActivity.this, SignInActivity.class);
                        startActivity(intent);

                    Log.d("lyjtest", "网络结果TIMER_TASK_COMPELETE:" + netresult);
                    break;
                case Tool.TIMER_TASK_INFO:
                    Log.d("lyjtest", "网络结果TIMER_TASK_INFO:"+netresult);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private Handler onCreateMessageHandler = new Handler() {
        public void handleMessage(Message msg) {
            Bundle b = msg.getData();
            String netresult = b.getString(Tool.TIMER_TASK_RESULT);
            switch (msg.what) {

                case Tool.TIMER_TASK_TIMEOUT:
                    Tool.showMsgDialog(SignInActivity.this, "线程超时"+netresult);
                    break;
                case Tool.TIMER_TASK_COMPELETE:
                    if(netresult.contains("未签到")){
                        userStatus.setText("未签到");
                        sigInButton.setText("上班签到");
                        signInAction="MorningSignIn.action" ;
                    }
                    if(netresult.contains("在上班")){
                        userStatus.setText("在上班");
                        sigInButton.setText("下班打卡");
                        signInAction="AfternoonEndSignIn.action";
                    }
                    if(netresult.contains("已下班")){
                        userStatus.setText("已下班");
                        sigInButton.setText("disable");
                        sigInButton.setClickable(false);
                    }

                    Log.d("lyjtest", "网络结果TIMER_TASK_COMPELETE:" + netresult);
                    break;
                case Tool.TIMER_TASK_INFO:
                    Log.d("lyjtest", "网络结果TIMER_TASK_INFO:"+netresult);
                    break;
            }
            super.handleMessage(msg);
        }
    };



}
